package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.annotation.WmLog;
import com.tm.dto.DishDTO;
import com.tm.dto.DishDTOD;
import com.tm.entity.CategoryEntity;
import com.tm.entity.DishEntity;
import com.tm.enums.OperationTypeEnum;
import com.tm.service.DishFlavorService;
import com.tm.service.DishService;
import com.tm.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangp
 * @create 2023-04-05 10:31
 */
@RestController
@RequestMapping("dish")
@CrossOrigin
@Api("菜品管理")
public class DishController {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @ApiOperation("查询菜品信息")
    @WmLog(operationModule = "查询", operationType = OperationTypeEnum.QUERY, description = "这是查询模块")
    @PostMapping("page")
    public Result page(@RequestParam(required = false, defaultValue = "1") Integer page,
                       @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                       DishDTO dishDTO) {
        Page<DishEntity> dishEntityPage = dishService.pageList(page, pageSize, dishDTO);
        return Result.success(dishEntityPage);

    }

    @DeleteMapping
    public Result deleteDish(@RequestParam("ids") List<Long> ids) {
        Boolean flag = dishService.removeByIds(ids);
        return Result.success(flag);
    }

    @PostMapping
    public Result saveWithFlavor(@RequestBody DishDTOD dishDTOD) {
        dishService.saveWithFlavor(dishDTOD);
        return Result.success("新增菜品成功");
    }

    /*@PostMapping("/status")
    public Result dishStatusByStatus(@RequestBody List<Long> ids) {
        Boolean flag = dishService.updateIds(ids);
        return Result.success(flag);
    }*/

    @GetMapping("/{id}")
    public Result getDishList(@PathVariable("id") Long id) {
        DishDTOD dishDTOD = dishService.getByIfwithFlavor(id);
        return Result.success(dishDTOD);
    }

    @ApiOperation("查询菜品信息")
    @WmLog(operationModule = "查询", operationType = OperationTypeEnum.QUERY, description = "这是查询模块")
    @GetMapping("list")
    public Result queryDishList(@RequestParam("categoryId") Integer categoryId) {

       /* QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<CategoryEntity>();
        queryWrapper.eq("type", type);
        List<CategoryEntity> categoryEntity = cateGoryService.list(queryWrapper);
        return Result.success(categoryEntity);*/

        QueryWrapper<DishEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(categoryId != null,"category_id",categoryId);
        List<DishEntity> dishEntity = dishService.list(wrapper);
        return Result.success(dishEntity);
    }

    /*@ApiOperation("修改菜品信息")
    @WmLog(operationModule = "修改", operationType = OperationTypeEnum.UPDATE, description = "这是修改模块")
    @PutMapping
    public Result editOrderDetail(@RequestBody DishEntity dishEntity) {
        UpdateWrapper<DishEntity> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id", dishEntity.getId());
        Boolean flag = dishService.update(dishEntity, UpdateWrapper);
        return Result.success(flag);
    }*/

    @PutMapping
    public Result editOrderDetail(@RequestBody DishDTOD dishDTOD) {

        dishService.updateWithFlavor(dishDTOD);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "批量修改菜品状态接口")
    public Result dishStatusByStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.dishStatusByStatus(status,ids);
        return Result.success();
    }

}
